package com.one.group.admin.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.RoleMapper;
import com.one.group.admin.mapper.RoleMenuMapper;
import com.one.group.model.dto.RoleQueryDto;
import com.one.group.model.dto.UpdateRoleMenuDto;
import com.one.group.model.dto.UpdateRolePermissionDto;
import com.one.group.model.entity.Role;
import com.one.group.model.entity.RoleMenu;
import com.one.group.model.vo.RoleListVo;
import com.one.group.service.RoleMenuService;
import com.one.group.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * {@code @Create:}  2025-09-28  12 : 53
 * {@code @Author:} Cherry
 * {@code @ToUser:} 凡是发生,皆有利于我。
 * ------------------------------------
 * {@code @note:}
 */
@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMenuMapper rolePermissionMapper;

    @Autowired
    private RoleMapper roleMapper;


    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public List<RoleListVo> findRoleByQueryDto(RoleQueryDto roleQueryDto) {
        //获取当前租户id
        Long tenant_id = Convert.toLong(StpUtil.getExtra("tenant_id"));
        return roleMapper.findRoleByQueryDto(roleQueryDto.getName(), tenant_id);
    }

    @Override
    public void updateRolePermission(UpdateRolePermissionDto updateRolePermissionDto) {

        //1.先删除角色对应的权限
        QueryWrapper lambdaQueryWrapper= QueryWrapper.create().from(RoleMenu.class);
        lambdaQueryWrapper.eq(RoleMenu::getRoleId,updateRolePermissionDto.getRoleId());
        rolePermissionMapper.deleteByQuery(lambdaQueryWrapper);

        //2.获取权限id集合
        List<Long> permissionIds = updateRolePermissionDto.getPermissionIds();


        //3.判断权限id集合不为空
        if(!CollectionUtils.isEmpty(permissionIds)){

            log.debug("王中间表插入数据...roleId:{},permissionIds:{}",
                    updateRolePermissionDto.getRoleId(),updateRolePermissionDto.getPermissionIds());
            permissionIds.forEach(permissionId->{
                RoleMenu rolePermission=new RoleMenu();
               rolePermission.setRoleId(updateRolePermissionDto.getRoleId());
               rolePermission.setMenuId(permissionId);
               rolePermissionMapper.insert(rolePermission);
            });

        }


    }


    @Override
    public void updateRoleMenu(UpdateRoleMenuDto updateRoleMenuDto) {
        // 删除当前角色所有菜单
        Long roleId = updateRoleMenuDto.getId();

        QueryWrapper queryWrapper = QueryWrapper.create().from(RoleMenu.class).eq("role_id", roleId);
        roleMenuService.remove(queryWrapper);

        if (updateRoleMenuDto.getMenuIds().size() > 0) {
            // 批量插入 role_menu
            List<Long> menuIds = updateRoleMenuDto.getMenuIds();
            List<RoleMenu> roleMenus = new ArrayList<RoleMenu>();
            for (Long menuId : menuIds) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setMenuId(menuId);
                roleMenu.setRoleId(roleId);
                roleMenus.add(roleMenu);
            }
            roleMenuService.saveBatch(roleMenus, roleMenus.size());
        }
    }

    @Override
    public List<String> findPermissionByAdminId(Long adminId) {
        return roleMapper.findPermissionByAdminId(adminId);
    }


    @Override
    public List<Role> findRoleByAdminId(Long adminId) {
        return roleMapper.findRoleByAdminId(adminId);
    }
}
